package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.modules.store.entity.TStoreRegionEntity;
import io.renren.modules.store.service.TStoreRegionService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreTypeEntity;
import io.renren.modules.store.service.TStoreTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-02-22 16:52:14
 */
@RestController
@RequestMapping("store/tstoretype")
public class TStoreTypeController extends AbstractController {
    @Autowired
    private TStoreTypeService tStoreTypeService;
    @Autowired
    private TStoreRegionService storeRegionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		TStoreTypeEntity tStoreType = tStoreTypeService.getById(id);

        return R.ok().put("tStoreType", tStoreType);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    public R save(@RequestBody TStoreTypeEntity tStoreType){
        TStoreTypeEntity one = tStoreTypeService.getOne(new QueryWrapper<TStoreTypeEntity>()
                .eq("store_type_name", tStoreType.getStoreTypeName())
                .eq("seller_id", getSeller())
        );
        if (one != null){
            throw new RRException("餐厅类型已存在，不能重复添加");
        }
        tStoreType.setSellerId(getSeller());
		tStoreTypeService.save(tStoreType);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TStoreTypeEntity tStoreType){
        TStoreTypeEntity one = tStoreTypeService.getOne(new QueryWrapper<TStoreTypeEntity>()
                .eq("store_type_name", tStoreType.getStoreTypeName())
                .eq("seller_id", getSeller())
                .notIn("id",tStoreType.getId())
        );
        if (one != null ){
            throw new RRException("餐厅类型已存在,请更换餐厅类型名字");
        }
		tStoreTypeService.updateById(tStoreType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        //判断下级是否分配了区域
        TStoreRegionEntity regionEntity = storeRegionService.getOne(new QueryWrapper<TStoreRegionEntity>()
            .eq("store_type_id",ids[0])
                .eq("seller_id",getSeller())
        );
        if (regionEntity != null){
            throw new RRException("此餐厅类型下级分配了区域,无法删除");
        }
        tStoreTypeService.removeById(ids[0]);
        return R.ok();
    }

}
